//
//  ResetNewPasswordViewController.swift
//  XGamesLive
//
//  Created by ios on 2024/5/14.
//

import UIKit
import MXCommonModule

class ResetNewPasswordViewController: UIViewController,CustomNavgationBar{
    lazy var hudView: XGL_CustomHudView = XGL_CustomHudView()
    var resetView: ResetNewPasswordView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.createCustomBackgroundView()
        self.resetView = ResetNewPasswordView().then({ make in
            make.delegate = self
        })
        self.view.addSubview(self.resetView)
        self.resetView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        self.creatClearNavigationBar(title: "Confirm Password")
    }

}

extension ResetNewPasswordViewController: ResetPasswordDelegate{
    func resetPassword() {
        guard self.resetView.password.isEmpty == false else {
            MXProgressHUD.hudWithText("Please enter new password")
            return
        }
        guard self.resetView.passwordSure.isEmpty == false else {
            MXProgressHUD.hudWithText("Please enter confirm new password")
            return
        }
        guard self.resetView.passwordSure == self.resetView.password else {
            MXProgressHUD.hudWithText("Two passwords do not match")
            return
        }
        self.hudView.hudView.startAnimation()
        self.view.addSubview(self.hudView)
        self.hudView.snp.makeConstraints { make in
            make.width.centerX.bottom.equalToSuperview()
            make.top.equalTo(PanNavibarH)
        }
        PanRequestManager<XGL_RequestBaseResponse<Response>,XGL_CustomRequestError>().request(CustomRequestAPI(requestAPI: .fakeRequest(strContent: self.resetView.password, intContent: nil))) { result in
            self.hudView.dismiss()
            switch result{
            case .success(let response):
                if let controllers = self.navigationController?.viewControllers,let controller = controllers.first(where: {$0.isKind(of: LoginWithEmailViewController.self)}){
                    self.navigationController?.popToViewController(controller, animated: true)
                }
                break
            default:
                break
            }
        }
    }
}
